package yl.hs.bmipfull.service.laboratorydata.imple;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmInspectionReportDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmMineralpowderReportItemDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmInspectionReportEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmMineralpowderInspectionItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmMineralpowderReportItemEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabRmMineralpowderReportItemService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 检测报告-矿粉报告服务层接口实现
 * @since 2022年07月01日 14时23分10秒 <br/>
 */
@Service
public class LabRmMineralpowderReportItemServiceImpl extends LaboratorydataBaseService implements ILabRmMineralpowderReportItemService {

    @Autowired
    ILabRmMineralpowderReportItemDao dao;
    @Autowired
    ILabRmInspectionReportDao reportDao;

    @Override
    public List<LabRmMineralpowderReportItemEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabRmMineralpowderReportItemEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<LabRmMineralpowderReportItemEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabRmMineralpowderReportItemEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabRmMineralpowderReportItemEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public LabRmMineralpowderReportItemEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabRmMineralpowderReportItem(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabRmMineralpowderReportItemEntity entity = null;
        LabRmInspectionReportEntity reportEntity=null;
        ObjectMapper objectMapper = new ObjectMapper();
        if (Tl.isEmpty(sn)) {
            //执行保存
            reportEntity = new LabRmInspectionReportEntity();
            Tl.getChangeLog(reportEntity, model);
            reportEntity.setPYFieldValue();
            reportEntity.setSn(queryNewSN(LabRmInspectionReportEntity.class));
            result = reportDao.insertNew(reportEntity);
            if (result > 0) {
                writeLog(ActionType.CREATE, reportEntity.getSn(), "插入检测报告-矿粉报告信息", Tl.toJson(reportEntity), LabRmInspectionReportEntity.class);
            }
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmMineralpowderReportItemEntity.class);
            subDataEntity.setReportSN(reportEntity.getSn());
            result = dao.insertNew(subDataEntity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新实验记录-矿粉实验值信息", Tl.toJson(subDataEntity), LabRmMineralpowderReportItemEntity.class);
            }
        } else {
            //执行修改
            reportEntity = reportDao.findBySN(sn);
            Tl.getChangeLog(reportEntity, model);
            reportEntity.setPYFieldValue();
            result = reportDao.update(reportEntity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新检测报告-矿粉报告信息", Tl.toJson(reportEntity), LabRmInspectionReportEntity.class);
            }
            dao.deleteByReportSN(reportEntity.getSn());
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmMineralpowderReportItemEntity.class);
            subDataEntity.setReportSN(reportEntity.getSn());
            result = dao.insertNew(subDataEntity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新实验记录-矿粉实验值信息", Tl.toJson(subDataEntity), LabRmMineralpowderReportItemEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabRmMineralpowderReportItemEntity.class);
        }
        return result;
    }

    @Override
    public List<LabRmMineralpowderReportItemEntity> getByReportSN(String reportSN) {
        return dao.getByReportSN(reportSN);
    }
}